//
// Created by francklinson on 2021/10/21 AT 15:35.
//
#include <iostream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <functional>
#include <climits>
#include <string>

using namespace std;

class Solution {
private:
    unordered_set<int> ans;
public:
    vector<int> twoOutOfThree(vector<int> &nums1, vector<int> &nums2, vector<int> &nums3) {
        // set 将数组扁平化 再找到两两之间的交集构成答案
        unordered_set<int> us1(nums1.begin(), nums1.end());
        unordered_set<int> us2(nums2.begin(), nums2.end());
        unordered_set<int> us3(nums3.begin(), nums3.end());
        // 找两两交集
        helper(us1, us2);
        helper(us1, us3);
        helper(us2, us3);
        vector<int> ret;
        for (auto &x: ans) {
            ret.push_back(x);
        }
        return ret;
    }

    void helper(unordered_set<int> &set1, unordered_set<int> &set2) {
        for (auto &x: set1) {
            if (set2.count(x))
                ans.insert(x);
        }
    }
    vector<int> twoOutOfThree2(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
        // 只有100个数字 可以用数组记录是否出现
        vector<int> res;
        int a[101]={0};
        int b[101]={0};
        int c[101]={0};
        for(int &num : nums1)  a[num] = 1;
        for(int &num : nums2)  b[num] = 1;
        for(int &num : nums3)  c[num] = 1;
        for(int i =0 ;i<101; i++){
            if(a[i] + b[i] + c[i] > 1) res.push_back(i);
        }
        return res;
    }
};

int main() {
    vector<int> nums1{1, 1, 3, 2};
    vector<int> nums2{2, 3};
    vector<int> nums3{3};
    Solution solution;
    vector<int> ans = solution.twoOutOfThree(nums1, nums2, nums3);
    for (auto &x: ans)
        cout << x << endl;
    return 0;
}

